package com.loopy.cookie;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UpdateServlet", value = "/update")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("UpdateServlet被调用");
        Cookie[] cookies = request.getCookies();
        Cookie cookie1 = CookieUtils.updateCookie("username", cookies);
        System.out.println("修改后的cookie值是");
        for (Cookie cookie : cookies) {
            System.out.println("cookieName="+cookie.getName()+"   cookieValue="+cookie.getValue());
        }
        if(cookie1!=null){
            response.addCookie(cookie1);
        }
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("<h1>更新cookie任务完成</h1>");
        writer.flush();
        writer.close();
    }
}
